<template>
	<view>
		<uni-list>
			<uni-list-item v-for="item in goodsList" :key='item.goods_id' clickable=true @click="enterDetail(item)">
				<template slot="header">
					<image class="slot-image" :src="item.goods_big_logo" mode="widthFix"></image>
				</template>
				<template slot="body" class="body-wrap">
					<text>{{item.goods_name}}</text>
					<text class="slot-text">￥{{item.goods_price}}</text>
				</template>
			</uni-list-item>
		</uni-list>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				goodsList: [],
				query: {
					query: "",
					cid: "",
					pagenum: 1,
					pagesize: 10,
					isloading: false
				},
				total: null
			};
		},
		created() {

		},
		onLoad(options) {
			this.query.query = options.query || ''
			this.query.cid = options.cid || ''
			this.getGoodsList()
		},
		onReachBottom() {
			if (this.query.pagenum * this.query.pagesize >= this.total) {
				uni.showToast({
					title: '没有更多了'
				})
				return
			}
			this.query.pagenum++
			if (this.isloading) {
				return
			}
			this.getGoodsList()
		},
		onPullDownRefresh() {
			this.query = {
				query: "",
				cid: "",
				pagenum: 1,
				pagesize: 10
			}
			this.isloading = false
			this.goodsList = []
			this.getGoodsList(() => uni.stopPullDownRefresh())
		},
		methods: {
			async getGoodsList(cb) {
				this.isloading = true
				uni.showLoading({
					title: '加载中...',
					mask: false
				});
				try {
					const { data: { message: { goods, total } } } = await uni.$http.get(`/api/public/v1/goods/search`,
						this
						.query)
					cb && cb()
					if (goods.length) {
						this.goodsList = [...this.goodsList, ...goods]
						this.total = total
					}
					uni.hideLoading()
				} catch (err) {
					uni.showToast({
						title: '没有更多了'
					})
				} finally {
					this.isloading = false
				}

			},
			enterDetail(item){
				uni.navigateTo({
					url:`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`
				})
			}
		}
	}
</script>

<style lang="scss">
	.slot-image {
		margin-right: 20rpx;
		width: 200rpx;
		height: 150rpx;
	}

	.body-wrap {
		display: flex;
		flex-direction: column;
		justify-content: space-between;

		.slot-text {
			color: #af5251;
		}
	}
</style>
